package com.ovopark.firecar.okhttp;


import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Map;

import io.reactivex.Observable;
import okhttp3.RequestBody;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

/**
 * Retrofit抽象封装服务类，结合Rxjava返回监听类Observable<JSONObject>
 * Created by poe on 2020/10/15
 */

public interface RetrofitService {

    /**
     * 返回JsonObject .
     * 一般的POST请求.
     * @param url
     * @param params
     * @return
     */
    @POST
    @DynamicTimeout(timeout = 30)
    @FormUrlEncoded
    Observable<JSONObject> postRequest(@Url String url, @FieldMap Map<String, String> params);

    /**
     * 上传文件.
     * @param url
     * @param params 文件列表.或者其他参数.
     * @return 正常返回.
     */
    @POST
    @DynamicTimeout(timeout = 30)
//    @Multipart
    Observable<JSONObject> postRequestUpload(@Url String url, @Body RequestBody params /*@Part List<MultipartBody.Part> parts*/, @HeaderMap Map<String, String> headers);

    /**
     * 返回JsonObject .
     * 带Headers的POST请求.
     * @param url
     * @param params
     * @return
     */
    @POST
    @DynamicTimeout(timeout = 30)
    @FormUrlEncoded
    Observable<JSONObject> postRequestHeaders(@Url String url, @FieldMap Map<String, String> params ,@HeaderMap Map<String, String> headers);


    /**
     * 通用构造RequestBody的请求，方便新token的raw登录方式.
     * 返回JsonObject .
     * @param url
     * @param params
     * @return
     */
    @POST
    @DynamicTimeout(timeout = 30)
    Observable<JSONObject> postRequest(@Url String url, @Body RequestBody params);

    /**
     * 同上，多一个Headers信息.
     * @param url
     * @param params
     * @param headers
     * @return
     */
    @POST
    @DynamicTimeout(timeout = 30)
    Observable<JSONObject> postRequest(@Url String url, @Body RequestBody params , @HeaderMap Map<String, String> headers);

    /**
     * 返回JsonObject .
     * @param url
     * @return
     */
    @POST
    @FormUrlEncoded
    Observable<JSONObject> postRequest(@Url String url);

    /**
     * 返回JsonObject .
     * @param url
     * @param params
     * @return
     */
    @POST
    @FormUrlEncoded
    Observable<JSONArray> postRequestJsonArray(@Url String url, @FieldMap Map<String, String> params, @HeaderMap Map<String, String> headers);

    /**
     * 返回String.
     * @param url
     * @param params
     * @return
     */
    @POST
    @FormUrlEncoded
    Observable<String> postSimpleRequest(@Url String url, @FieldMap Map<String, String> params);

    /**
     * 返回String .
     * @param url
     * @return
     */
    @POST
    @FormUrlEncoded
    Observable<String> postSimpleRequest(@Url String url);


    /**
     * 返回String .
     * @param url
     * @param params
     * @return
     */
    @POST
    @FormUrlEncoded
    Observable<JSONObject> postSimpleRequestHeaders(@Url String url, @FieldMap Map<String, String> params ,@HeaderMap Map<String, String> headers);


    /**
     * 返回JsonObject .
     * @param url
     * @param headers
     * @return
     */
    @GET
    Observable<JSONObject> getRequest(@Url String url, @HeaderMap Map<String, String> headers);




    /**
     * 返回JsonObject .
     * @param url
     * @param params
     * @return
     */
    @GET
    Observable<JSONObject> getRequestHeaders(@Url String url, @QueryMap Map<String, String> params, @HeaderMap Map<String, String> headers);

    /**
     * 返回JsonObject .
     * @param url
     * @return
     */
    @GET
    Observable<JSONObject> getRequest(@Url String url);


    /**
     * 返回JsonObject .
     * @param url
     * @return
     */
    @GET
    Observable<JSONArray> getRequestJsonArray(@Url String url);

    /**
     * 返回String.
     * @param url
     * @param params
     * @return
     */
    @GET
    Observable<String> getSimpleRequest(@Url String url, @FieldMap Map<String, String> params);

    /**
     * 返回String.
     * @param url
     * @param params
     * @return
     */
    @GET
    Observable<String> getSimpleRequestHeaders(@Url String url, @FieldMap Map<String, String> params,@HeaderMap Map<String, String> headers);

    /**
     * 返回String .
     * @param url
     * @return
     */
    @GET
    Observable<String> getSimpleRequest(@Url String url);


    /**
     * 新token的登录接口.
     */
//    public static final String TOKEN_LOGIN_NEW = "user/login";

    /**
     * 新登录.
     * 返回JsonObject .
     * 带Headers的POST请求.
     * @param groupId 企业id
     * @param loginType 登录类型1web 2ios 3Android
     * @return
     */
    @POST("user/login")
    @DynamicTimeout(timeout = 30)
    @FormUrlEncoded
    @Headers("Content-Type: application/json")
    Observable<JSONObject> login(@Field("userName") String userName, @Field("password") String password, @Field("groupId") String groupId, @Field("loginType") String loginType);

    /**
     * 刷新token.
     */
//    public static final String TOKEN_LOGIN_NEW_REFRESH = "user/reFreshToken";
    @GET("user/reFreshToken")
    @Headers("Content-Type: application/json")
    Observable<JSONObject> refreshToken(@Query("token") String token);


    /**
     * 返回JsonObject .
     * @param url
     * @param params
     * @return
     */
    @PUT
    Observable<JSONObject> putRequest(@Url String url, @HeaderMap Map<String, String> params);
}
